<?php

namespace app\controllers;

use Yii;
use yii\base\Exception;
use yii\data\ActiveDataProvider;
use yii\data\SqlDataProvider;
use app\com\simtoon\log\LogUtils;
/**
 * 序列化类
 */
class SerializerToJSon implements SerializerModels{

    /**
     * 序列化JSON方法
     * @param $model
     * @return mixed
     */
    public function parseJson($result){
        try{
            $res = array();
            if(count($result) > 0){

                $res['status'] = 0;
                if($result == '1' || $result == '0') {
                    $res['res'] = $result;
                }else if($result == '-1'){
                    $res['status'] = 1;
                    $res['msg'] = '操作失败!';
                }else{

                    if($result instanceof ActiveDataProvider){

                        $pagin = $result->getPagination();
                        if($pagin->totalCount >= 0){
                            $res['pageCount'] = $pagin->totalCount;
                            $res['pageNow'] = $pagin->pageSizeLimit[1];
                            $res['pageSize'] = $pagin->pageSizeLimit[0];
                        }

                            $model = $result->getModels();
                            if(count($model) > 0){
                                foreach ($model as $key => $value) {
                                    $ml = $value->getoldAttributes();
                                    $arr = array();
                                    foreach ( array_keys($ml) as $field => $definition) {
                                        $arr[$definition] = is_string($definition) ? $value->$definition : call_user_func($definition, $value, $definition);
                                    }



//                                    $arr = array();
//                                    foreach($ml as $k=>$v){
//                                        $arr[$k] = $v;
//                                    }
                                    $array[$key] = $arr;
                                }
                                $res['result'] = $array;
                            }else{
                                $res['result'] = array();
                            }
                    }else if($result instanceof SqlDataProvider){

                        $pagin = $result->getPagination();
                        if($pagin->totalCount >= 0){
                            $res['pageCount'] = $pagin->totalCount;
                            $res['pageNow'] = $pagin->pageSizeLimit[1];
                            $res['pageSize'] = $pagin->pageSizeLimit[0];
                        }

                            $model = $result->getModels();
                            if(count($model) > 0){
                                foreach ($model as $key => $value) {
                                        if(is_array($value)){
                                            $arr = array();
                                            foreach($value as $k=>$v){
                                                $arr[$k] = $v;
                                            }
                                            $array[$key] = $arr;
                                        }else{
                                            $array[$key] = $value;
                                        }
                                }
                                $res['result'] = $array;
                            }else{
                                $res['result'] = array();
                            }

                    }else{
                        if(count($result) >= 0){
                            $array = "";
                            foreach ($result as $key => $value) {
                                if(is_array($value)){
                                    $arr = array();
                                    foreach($value as $k=>$v){
                                        /**
                                         * 2015.11.25  yangliang  若$v为Array则进行遍历，否则直接赋值
                                         */
                                        if(is_array($v)){
                                            $ar = array();
                                            foreach($v as $ky=>$vl){
                                                /**
                                                 * 2015.11.25  yangliang  若$vl为Array则遍历数组，否则直接赋值
                                                 */
                                                if(is_array($vl)){
                                                    $arra = array();
                                                    foreach($vl as $ke=>$va){
                                                        $arra[$ke] = $va;
                                                    }
                                                    $ar[$ky] = $arra;
                                                }else{
                                                    $ar[$ky] = $vl;
                                                }
                                            }
                                            $arr[$k] = $ar;
                                        }else{
                                            $arr[$k] = $v;
                                        }
                                    }
                                    $array[$key] = $arr;
                                }else{
                                    $array[$key] = $value;
                                }
                            }
                            $res['result'] = $array;
                        }else{
                            $res['result'] = array();
                        }
                    }
                }
            }
            else if(count($result) === 0){
                $res['status'] = 0;
                $res['result'] = array();
            }
            else{
                $res['status'] = 1;
                $res['msg'] = '查询失败!';
            }

           $ss =  json_encode($res);
           die($ss);
        }catch (Exception $e){
            LogUtils::error($e->getMessage(), __METHOD__);
            die(json_encode($res));
        }
    }

}

?>